package com.lancoo.znbkxx.teacher.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lancoo.znbkxx.teacher.R;

import java.math.BigDecimal;


public class CustomTextViewToPicture {

    private Context context;
    private TextView tv;

    public CustomTextViewToPicture(Context context, TextView tv, String source) {
        this.context = context;
        this.tv = tv;
        MyImageGetter myImageGetter = new MyImageGetter();
        tv.setText(Html.fromHtml(source, myImageGetter, null));
    }

    class MyImageGetter implements Html.ImageGetter {

        @Override
        public Drawable getDrawable(String source) {
            MyDrawableWrapper myDrawable = new MyDrawableWrapper();
            Drawable drawable = context.getResources().getDrawable(R.mipmap.aibk_teacher_launcher);
            drawable.setBounds(
                    0,
                    0,
                    drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            myDrawable.setDrawable(drawable);
            Glide.with(context)
                    .asBitmap()
                    .load(source)
                    .into(new BitmapTarget(myDrawable));
            return myDrawable;
        }
    }


    class BitmapTarget extends SimpleTarget<Bitmap> {
        public final MyDrawableWrapper myDrawable;

        public BitmapTarget(MyDrawableWrapper myDrawableX) {
           myDrawable = myDrawableX;
        }

        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
            Drawable drawable = new BitmapDrawable(context.getResources(), resource);
            //获取原图大小
            int width = drawable.getIntrinsicWidth();
            int height = drawable.getIntrinsicHeight();
            //自定义drawable的高宽, 缩放图片大小最好用matrix变化，可以保证图片不失真
//            drawable.setBounds(0, 0, 300, 300);
//            myDrawable.setBounds(0, 0, 300, 300);

            Log.e("BitmapTarget", "==============width============" + width);
            Log.e("BitmapTarget", "==============height============" + height);

//            if (width > height) {
//                if (width < 400) {
//                    height = (int)(round(400,width,2)*height);
//                    width = 400;
//                }
//            } else {
//                if (height < 400) {
//                    width = (int)(round(400,height,2)*width);
//                    height = 400;
//                }
//            }

            height = 300;
            width = 400;

            drawable.setBounds(0, 0, width, height);
            myDrawable.setBounds(0, 0, width, height);
            myDrawable.setDrawable(drawable);
            tv.setText(tv.getText());
            tv.invalidate();
        }
    }

    private double round(int a,int b, int scale) {
//        double f1 = new BigDecimal((float)a/b).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
        return new BigDecimal((float)a/b).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
